package cn.vconinfo.app.response;


import cn.vconinfo.app.enums.BusinessService;

import java.io.Serializable;

/**
 * @author vconinfo
 * @createdOn 2019/07/22
 * @description: 接口返回工具
 */
public class CommonReturn<T> implements Serializable {
    private static final long serialVersionUID = 7142699374912032444L;
    T data;
    private int code;
    private String error;
    private String msg;
    public CommonReturn(T data) {
        this.generateCommonReturn(data, BusinessService.CommonBusinessCode.REQ_SUCCESS.getCode(),
                BusinessService.CommonBusinessCode.REQ_SUCCESS.getCodeStr(),
                BusinessService.CommonBusinessCode.REQ_SUCCESS.getMsg());
    }

    /**
     *
     * @param data
     * @param code
     * @param error
     * @param msg
     */
    public CommonReturn(T data, int code, String error, String msg) {
        this.generateCommonReturn(data, code, error, msg);
    }

    /**
     *
     * @param data
     * @param businessCode
     */
    public CommonReturn(T data, BusinessService.CommonBusinessCode businessCode) {
        if (businessCode != null) {
            this.generateCommonReturn(data, businessCode.getCode(), businessCode.getCodeStr(), businessCode.getMsg());
        } else {
            this.generateCommonReturn(data, BusinessService.CommonBusinessCode.REQ_SUCCESS.getCode(),
                    BusinessService.CommonBusinessCode.REQ_SUCCESS.getCodeStr(),
                    BusinessService.CommonBusinessCode.REQ_SUCCESS.getMsg());
        }

    }
    public void generateCommonReturn(T data, int code, String error, String msg) {
        this.setData(data);
        this.setCode(code);
        this.setError(error);
        this.setMsg(msg);
    }
    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
